package controllers

import (
    "encoding/json"
    "github.com/beego/beego/v2/server/web"
    "volux/utils"
)

type HostController struct {
    web.Controller
}

type Host struct {
    NQN         string   `json:"nqn"`
    Description string   `json:"description"`
    Status      string   `json:"status"`
}

func (c *HostController) AddHost() {
    nqn := c.Ctx.Input.Param(":nqn")
    if nqn == "" {
        utils.ErrorResponse(c.Controller, 400, "NQN is required")
        return
    }

    var host Host
    if err := json.Unmarshal(c.Ctx.Input.RequestBody, &host); err != nil {
        utils.ErrorResponse(c.Controller, 400, "Invalid request body")
        return
    }

    spdkClient := utils.GetSPDKClient()
    if spdkClient == nil {
        utils.ErrorResponse(c.Controller, 500, "SPDK client not available")
        return
    }

    err := spdkClient.AddHostToSubsystem(nqn, host.NQN)
    if err != nil {
        utils.ErrorResponse(c.Controller, 500, "Failed to add host: "+err.Error())
        return
    }

    utils.SuccessResponse(c.Controller, host)
}

func (c *HostController) DeleteHost() {
    nqn := c.Ctx.Input.Param(":nqn")
    hostNQN := c.Ctx.Input.Param(":host_nqn")
    if nqn == "" || hostNQN == "" {
        utils.ErrorResponse(c.Controller, 400, "NQN and host NQN are required")
        return
    }

    spdkClient := utils.GetSPDKClient()
    if spdkClient == nil {
        utils.ErrorResponse(c.Controller, 500, "SPDK client not available")
        return
    }

    err := spdkClient.RemoveHostFromSubsystem(nqn, hostNQN)
    if err != nil {
        utils.ErrorResponse(c.Controller, 500, "Failed to remove host: "+err.Error())
        return
    }

    utils.SuccessResponse(c.Controller, map[string]string{
        "message": "Host removed successfully",
    })
}

func (c *HostController) GetHosts() {
    nqn := c.Ctx.Input.Param(":nqn")
    if nqn == "" {
        utils.ErrorResponse(c.Controller, 400, "NQN is required")
        return
    }

    spdkClient := utils.GetSPDKClient()
    if spdkClient == nil {
        utils.ErrorResponse(c.Controller, 500, "SPDK client not available")
        return
    }

    subsystems, err := spdkClient.GetSubsystems()
    if err != nil {
        utils.ErrorResponse(c.Controller, 500, "Failed to get hosts")
        return
    }

    // 从subsystems中提取指定NQN的主机列表
    var hosts []Host
    subsystemsList := subsystems.([]interface{})
    for _, sub := range subsystemsList {
        subsystem := sub.(map[string]interface{})
        if subsystem["nqn"].(string) == nqn {
            if hostsList, ok := subsystem["hosts"].([]interface{}); ok {
                for _, h := range hostsList {
                    host := h.(map[string]interface{})
                    hosts = append(hosts, Host{
                        NQN:         host["nqn"].(string),
                        Description: "",
                        Status:      "connected",
                    })
                }
            }
            break
        }
    }

    utils.SuccessResponse(c.Controller, hosts)
}
